<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('新增通知通告')"/>
    <th:block th:include="include :: summernote-css"/>
    <th:block th:include="include :: bootstrap-select-css"/>
    <th:block th:include="include :: ztree-css"/>
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-notice-add">
        <div class="form-group">
            <label class="col-sm-2 control-label is-required">公告标题：</label>
            <div class="col-sm-10">
                <input id="notifyTitle" name="notifyTitle" class="form-control" type="text" required>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label">公告类型：</label>
            <div class="col-sm-1">
                <select name="notifyType" class="form-control m-b" th:with="type=${@dict.getType('oa_notify_type')}">
                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label">公告内容：</label>
            <div class="col-sm-10">
                <input id="notifyContent" name="notifyContent" type="hidden">
                <div class="summernote"></div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label">公告状态：</label>
            <div class="col-sm-10">
                <div class="radio-box" th:each="dict : ${@dict.getType('oa_notify_status')}">
                    <input type="radio" th:id="${dict.dictCode}" name="status" th:value="${dict.dictValue}"
                           th:checked="${dict.default}">
                    <label th:for="${dict.dictCode}" th:text="${dict.dictLabel}"></label>
                </div>
                <i style="color: #d41919">注意：发布后不能进行操作。</i>
            </div>
        </div>
        <div id="recipient" class="form-group">
            <label class="col-sm-2 control-label">接收人：</label>
            <div class="col-sm-10">
                <label class="check-box">
                    <input type="checkbox" value="2" checked>展开/折叠</label>
                <label class="check-box">
                    <input type="checkbox" value="3">全选/全不选</label>
                <label class="check-box">
                    <input type="checkbox" value="4" checked>父子联动</label>
                <div id="deptUserTrees" class="ztree ztree-border"></div>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: summernote-js"/>
<th:block th:include="include :: bootstrap-select-js"/>
<th:block th:include="include :: ztree-js"/>
<script type="text/javascript" th:inline="javascript">
    var prefix = ctx + "oa/notify";

    $(function () {
        //通告状态
        notifyStatus();

        //初始化部门人员树
        extracted();

    });

    function extracted() {
        var url = prefix + "/deptUserTreeData?userIds=" + $("#userIds").val();
        var options = {
            id: "deptUserTrees",
            url: url,
            check: {enable: true},
            expandLevel: 4
        };
        $.tree.init(options);
    }

    $('input').on('ifChanged', function (obj) {
        var type = $(this).val();
        var checked = obj.currentTarget.checked;
        if (type == '2') {
            if (checked) {
                $._tree.expandAll(true);
            } else {
                $._tree.expandAll(false);
            }
        } else if (type == "3") {
            if (checked) {
                $._tree.checkAllNodes(true);
            } else {
                $._tree.checkAllNodes(false);
            }
        } else if (type == "4") {
            if (checked) {
                $._tree.setting.check.chkboxType = {"Y": "ps", "N": "ps"};
            } else {
                $._tree.setting.check.chkboxType = {"Y": "", "N": ""};
            }
        }
    })

    function notifyStatus() {
        //默认接收人tab隐藏
        $("#recipient").hide();
        $('input').on('ifChecked', function (event) {
            var status = $(event.target).val();
            if (status == "0") {//草稿
                $("#recipient").hide();
            } else if (status == "1") {//发布
                $("#recipient").show();
            }
        });
    }

    $('.summernote').summernote({
        toolbar: [
            //编辑
            ['edit',['undo','redo']],
            //标题
            ['headline', ['style']],
            //样式
            ['style', ['bold', 'italic', 'underline', 'superscript', 'subscript', 'strikethrough', 'clear']],
            //字体
            ['fontface', ['fontname','color','fontsize']],
            //对齐方式
            ['alignment', ['ul', 'ol', 'paragraph', 'lineheight']],
            //行高
            ['height', ['height']],
            //表格
            ['table', ['table']],
            //插入：链接，图片，视频，水平线
            ['insert', ['link','picture','video','hr']],
            //全屏，代码视图
            ['view', ['fullscreen', 'codeview']]
        ],
        placeholder: '请输入公告内容',
        height: 320,
        lang: 'zh-CN',
        followingToolbar: false,
        dialogsInBody: true,
        lineHeights:['0.2', '0.3', '0.4', '0.5', '0.6', '0.8', '1.0', '1.2', '1.4', '1.5', '2.0', '3.0'],
        fontNames: ['宋体','微软雅黑','楷体','黑体','隶书','Arial', 'Arial Black', 'Comic Sans MS', 'Courier New', 'Helvetica Neue', 'Helvetica', 'Impact',
            'Lucida Grande', 'Tahoma', 'Times New Roman', 'Verdana'],
        addDefaultFonts: false,
        callbacks: {
            onImageUpload: function (files) {
                sendFile(files[0], this);
            }
        }
    });

    // 上传文件
    function sendFile(file, obj) {
        var data = new FormData();
        data.append("file", file);
        $.ajax({
            type: "POST",
            url: ctx + "common/upload",
            data: data,
            cache: false,
            contentType: false,
            processData: false,
            dataType: 'json',
            success: function (result) {
                if (result.code == web_status.SUCCESS) {
                    $(obj).summernote('editor.insertImage', result.url, result.fileName);
                } else {
                    $.modal.alertError(result.msg);
                }
            },
            error: function (error) {
                $.modal.alertWarning("图片上传失败。");
            }
        });
    }

    $("#form-notice-add").validate({
        focusCleanup: true
    });

    function submitHandler() {
        if ($.validate.form()) {
            var sHTML = $('.summernote').summernote('code');
            $("#notifyContent").val(sHTML);

            var data = $("#form-notice-add").serializeArray();
            //获取选中的节点
            var userIds  = $.tree.getCheckedNodes();

            //如果是发布状态，提示选择接收人
            if($(':radio[name=status]:checked').val() == '1'){
                if(userIds==''){
                    $.modal.alertError("请选择接收人！");
                    return false;
                }
            }

            data.push({"name": "userIds", "value": userIds});
            $.operate.save(prefix + "/add", data);
        }
    }

    function doSubmit(index, layero) {
        var tree = layero.find("iframe")[0].contentWindow.$._tree;
        var body = $.modal.getChildFrame(index);
        $("#treeId").val(body.find('#treeId').val());
        $("#treeName").val(body.find('#treeName').val());
        $.modal.close(index);
    }
</script>
</body>
</html>
